<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\News as NewsModel;
use app\user\model\Language as LanguageModel;
/**
 * 新闻控制器
 * @package app\user\admin
 */
class News extends Admin
{
    /**
     * 消息列表
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = NewsModel::where($this->getMap())
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->setTableName('admin_news')
            ->addTopButton('add',['title'=>'add'])
            ->addTopButton('delete',['title'=>'delete'])
            ->addRightButton('edit',['title'=>'edit'])
            ->addRightButton('delete',['title'=>'delete'])
            ->setPageTitle('News')
            ->addColumns([
                ['id', 'ID'],
                ['title_en', 'title'],
                ['cover_image','Cover','picture'],
                ['type', 'Category','select',config('setting.category')],
                ['status', 'Status', 'switch'],
                ['create_time', 'Create Time'],
                ['right_button', 'Operation', 'btn'],
            ])
            ->addFilter('type',config('setting.category'))
            ->addFilterList('status',['disable','enable'])
            ->setSearch(['title_en'=>'title'])
            ->setColumnWidth([
                'title_en'  => 200,
                'status'  => 80,
                'create_time'  => 150,
                'id'=>50])
            ->setRowList($data_list)
            ->fetch();
    }

    /**
     * 新增
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $data['title_en'] == '' && $this->error('Please fill in the news title');
            $data['content_en'] == '' && $this->error('Please fill in the news content');
            $data['sub_title'] == '' && $this->error('Please fill in the news sub title');

            $list = $data;
            unset($list['__token__']);
            $list['create_time']=date('Y-m-d H:i:s');

            $MessageModel = new NewsModel();
            if (false !== $MessageModel->save($list)) {
                $this->success('add success', 'index');
            } else {
                $this->error('add failed');
            }
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','title_en','Title'],
                ['textarea','sub_title','Sub Title'],
                ['ueditor','content_en','Content'],
                ['image','cover_image','Cover'],
                ['select', 'type', 'Category', '', config('setting.category'), '1', '']
            ])
            ->fetch();
    }

    public function edit($id = 0)
    {
        if ($id === 0) $this->error('missing parameter');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post('', null, 'trim');

            // 验证
            $data['title_en'] == '' && $this->error('Please fill in the news title');
            $data['content_en'] == '' && $this->error('Please fill in the news content');
            $data['sub_title'] == '' && $this->error('Please fill in the news sub title');
            if (NewsModel::update($data,['id'=>$id])) {
                $this->success('edit success', 'index');
            } else {
                $this->error('edit failed');
            }
        }

        // 获取数据
        $info = NewsModel::get($id);

        // 使用ZBuilder快速创建表单
//        $tabs=[];uppercase
//        $languages=get_languages();
//        foreach ($languages as $key=>$language){
//            $tabs[$language['name']][]=['textarea','title_'.$language['sign'],$language['name'].'标题'];
//            $tabs[$language['name']][]=['ueditor','content_'.$language['sign'],$language['name'].'内容'];
//        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','title_en','Title'],
                ['textarea','sub_title','Sub Title'],
                ['ueditor','content_en','Content'],
                ['image','cover_image','Cover'],
                ['select', 'type', 'News Category', '', [1=>'all',2=>'financial'], '1', '']
            ])
            ->setFormData($info)
            ->fetch();
    }
}
